package com.sfx.concurrent.demo.concurrent.ThreadPoll;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-25
 * Time: 18:55
 */
@Slf4j(topic = "c.MyTestThreadPool")
public class MyTestThreadPool {

    public static void main(String[] args) {
        MyThreadPool threadPool = new
                MyThreadPool(1,1000,
                TimeUnit.MILLISECONDS,1,
                    (queue,task)->{
                        /**
                         * 等任务队列满的情况下,执行拒绝策略
                         */
                        //1.死等
                        //queue.put(task);
                        //2.超时等待
                        queue.offer(task,500,TimeUnit.MILLISECONDS);
                        //3.放弃任务
                        //log.debug("放弃任务");
                        //4.抛出异常
                        //throw new RuntimeException("任务队列已满");
                        //5.调用者自己调用任务
                        //task.run();
                    }
                );
        for(int i =0;i<3;++i) {
            int j = i;
            threadPool.execute(()->{
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("{}",j);
            });
        }
    }

}
